package com.csx.base.utils;

import com.alibaba.fastjson.JSONObject;
import com.csx.base.meta.OdsColumn;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.JsonObject;
import com.yh.csx.bsf.core.util.ConvertUtils;
import com.yh.csx.bsf.core.util.JsonUtils;
import com.yh.csx.bsf.core.util.StringUtils;
import lombok.var;

import java.util.Date;

public class CRCUtils {
	public static void checkEqualData(Object from, Object to, OdsColumn column) {
		if(from==null&&to==null){
			return;
		}
		var str1 = "";
		var str2 = "";
		var colType = StringUtils.nullToEmpty(column.getColumnType()).toLowerCase();
		if(colType.contains("tinyint(1)")){
			str1=Boolean.toString(Math.abs(ConvertUtils.convert(from,Integer.class))>=1?true:false).toLowerCase();
			str2=ConvertUtils.convert(to,Boolean.class).toString().toLowerCase();
		}
		else if(colType.contains("decimal"))
		{
			from = (from == null?"0":from);
			to = (to == null?"0":to);

			str1 = ConvertUtils.convert(from, Double.class).toString();
			str2=ConvertUtils.convert(to,Double.class).toString();
		}
		else if(colType.contains("datetime")){
			var d1= ConvertUtils.convert(from, Date.class);
			var d2 = ConvertUtils.convert(to,Date.class);
			str1=(d1==null?"":d1.toString());
			str2=(d2==null?"":d2.toString());
		}
		else if(colType.contains("json")){
			if(from==null&&to==null){
				str1="";
				str2="";
			}
			else {
				str1 = ConvertUtils.convert(from, String.class);
				str2 = ConvertUtils.convert(to, String.class);
				var j1 = JSONObject.parse(str1);
				var j2 = JSONObject.parse(str2);
				if ((j1==null&&j2==null)||(j1!=null&&j1.equals(j2))) {
					str1 = "【json对象】";
					str2 = "【json对象】";
				}
			}
		}
		else{
			 str1 = ConvertUtils.convert(from,String.class);
			 str2 = ConvertUtils.convert(to,String.class);
		}

		str1=filter(StringUtils.nullToEmpty(str1));
		str2=filter(StringUtils.nullToEmpty(str2));
		if(str1.equals(str2)){
		}else
		{
			if(str1.length()<100){
				throw new RuntimeException(str1+" -> "+str2);
			}
			else{
				throw new RuntimeException(StringUtils.subString3(str1,10)+" -> "+StringUtils.subString3(str2,10));
			}
		}
	}

	private static String filter(String cnt){
		return cnt.replace("\\","");
	}




}
